<?php
/**
 * Created by PhpStorm.
 * User: liu
 * Date: 2018/5/16
 * Time: 0:18
 */

namespace app\helper;


/*
 * 用于腾讯云的查询解析api
 */
use app\common\except\Common;
use think\facade\Env;

class TxApi
{
    const URL='cns.api.qcloud.com';
    protected $secretId;
    protected $secretKey;
    protected $publicParam;
    public function __construct()
    {
        $this->secretId= Env::get('txApi.secretId');
        $this->secretKey=Env::get('txApi.secretKey');
        if(is_null($this->secretId)|| is_null($this->secretKey)){
            throw new Common('腾讯云sk/sid不能为null');
        }
        $this->publicParam=[
            'Timestamp'=>time(),
            'Nonce'=>mt_rand(10000,100000),
            'SecretId'=>$this->secretId,
        ];
    }
    public function sign($param,$method,$domain,$url='/v2/index.php',$hash='sha1'){
        $data=[];
        foreach ($param as $k=>$v){
            $data[]="{$k}={$v}";
        }
        $string=implode('&',$data);
        $string=strtoupper($method).$domain.$url.'?'.$string;
        $signStr=base64_encode(hash_hmac($hash,$string,$this->secretKey,true));
        return $signStr;
    }
    public function record_list($offset=0,$length=20){
        $param=[
            'domain'=>'cns.api.qcloud.com',
            'Action'=>'DomainList',
            'Timestamp'=>time(),
            'Nonce'=>mt_rand(10000,100000),
            'SecretId'=>$this->secretId,
            'offset'=>$offset,
            'length'=>$length
        ];

        ksort($param);
        $sign=$this->sign($param,'get',$param['domain']);
        $param['Signature']=$sign;
        $url="https://{$param['domain']}/v2/index.php?";
        $query_url=http_build_query($param);
        $response=\Requests::get($url.$query_url);
        $response_json=json_decode($response->body,true);
        if($response_json['code']==0){
            var_dump($response_json);
        }
    }
    public function record_create($param){
        $param=array_merge($param,$this->publicParam);
        ksort($param);
        $sign=$this->sign($param,'get',self::URL);
        $param['Signature']=$sign;
        $url='https://'.$this::URL.'/v2/index.php?';
        $query_url=http_build_query($param);
        $response=\Requests::get($url.$query_url);
        $response_json=json_decode($response->body,true);
        if($response_json['code']==0){
            return $response_json['data']['record'];
        }
        throw new Common($response_json['message'],300);
    }
    public function recode_update($param){
        $param=array_merge($param,$this->publicParam);
        ksort($param);
        $sign=$this->sign($param,'get',self::URL);
        $param['Signature']=$sign;
        $url='https://'.$this::URL.'/v2/index.php?';
        $query_url=http_build_query($param);
        $response=\Requests::get($url.$query_url);
        $response_json=json_decode($response->body,true);
        if($response_json['code']==0){
            return $response_json['data']['record'];
        }
        var_dump($response_json);exit;
        throw new Common($response_json['message'],300);
    }
    public function recode_remove($param){
        $param=array_merge($param,$this->publicParam);
        ksort($param);
        $sign=$this->sign($param,'get',self::URL);
        $param['Signature']=$sign;
        $url='https://'.$this::URL.'/v2/index.php?';
        $query_url=http_build_query($param);
        $response=\Requests::get($url.$query_url);
        $response_json=json_decode($response->body,true);
        if($response_json['code']!==0){
            throw new Common($response_json['message'],300);
        }
    }
}